/**
 * Created by Administrator on 2017/3/1 0001.
 */
var Scroller={
    create:function (target,pageWidth,pageHeight,pageDir,arr_page) {
        var sc={pageIndex:0,pageMax:arr_page.length-1};
        var bool_down;

        var mouseDownPT={x:0,y:0};
        var targetStartPT={x:0,y:0};
        var threhold=100;
        var tw_page;

        target.interactive=true;
        target.on('pointerdown',downHandler);
        target.on('pointerup', upHandler)
        target.on('pointermove',moveHandler);

        sc.scrollToPage=function (pageIdx) {

        }
        sc.scrollToPosition=function (x,y) {

        }
        for(var z=0;z<arr_page.length;z++) {
            if (pageDir == "ver") {
                arr_page[z].ReturnWhole().y=pixiStageH*z;
            }
            else {
                arr_page[z].ReturnWhole().x=pixiStageW*z;
            }
        }

        function downHandler(e) {
            bool_down=true;
            if (tw_page) {
                tw_page.stop();
                TWEEN.remove(tw_page);
                tw_page=null;
            }
            mouseDownPT.x = e.data.global.x;
            mouseDownPT.y = e.data.global.y;
            targetStartPT.x = target.x;
            targetStartPT.y = target.y;
        }
        function upHandler(e) {
            bool_down = false;
            if(pageDir=='ver') {
                if (target.y < -sc.pageIndex * pageHeight - threhold) {
                    if (sc.pageIndex != sc.pageMax) {
                        target.interactive = false;
                        var event = new CustomEvent("Game_ScrollStart", {detail:sc.pageIndex });
                        document.dispatchEvent(event);

                        TweenMax.to(target,0.7,{y:-pageHeight * (sc.pageIndex + 1),ease:Back.easeOut,onComplete:function () {
                            sc.pageIndex++;
                            target.interactive = true;
                            var event = new CustomEvent("Game_ScrollComplete", {detail: sc.pageIndex});
                            document.dispatchEvent(event);
                        }});
                    }
                    else {
                        back();
                    }

                }
                else if (target.y > -sc.pageIndex * pageHeight + threhold) {
                    if (sc.pageIndex != 0) {
                        target.interactive = false;
                        var event = new CustomEvent("Game_ScrollStart", {detail:sc.pageIndex });
                        document.dispatchEvent(event);

                        TweenMax.to(target,0.7,{y:-pageHeight * (sc.pageIndex - 1),ease:Back.easeOut,onComplete:function () {
                            sc.pageIndex--;
                            target.interactive = true;
                            var event = new CustomEvent("Game_ScrollComplete", {detail: sc.pageIndex});
                            document.dispatchEvent(event);
                        }});
                    }
                    else {
                        back();
                    }
                }

                else {
                    back();
                }
            }
            else if(pageDir=='hor')
            {

                if (target.x < -sc.pageIndex * pageWidth - threhold) {
                    if (sc.pageIndex != sc.pageMax) {
                        target.interactive = false;
                        var event = new CustomEvent("Game_ScrollStart", {detail:sc.pageIndex });

                        TweenMax.to(target,0.7,{x:-pageWidth * (sc.pageIndex + 1),ease:Back.easeOut,onComplete:function () {
                            sc.pageIndex++;
                            target.interactive = true;
                            var event = new CustomEvent("Game_ScrollComplete", {detail: sc.pageIndex});
                            document.dispatchEvent(event);
                        }});
                    }
                    else {
                        back();
                    }
                }
                else if (target.x > -sc.pageIndex * pageWidth + threhold) {
                    if (sc.pageIndex != 0) {
                        target.interactive = false;
                        var event = new CustomEvent("Game_ScrollStart", {detail:sc.pageIndex });
                        document.dispatchEvent(event);

                        TweenMax.to(target,0.7,{x:-pageWidth * (sc.pageIndex - 1),ease:Back.easeOut,onComplete:function () {
                            sc.pageIndex--;
                            target.interactive = true;
                            var event = new CustomEvent("Game_ScrollComplete", {detail:sc.pageIndex });
                            document.dispatchEvent(event);
                        }});
                    }
                    else {
                        back();
                    }
                }

                else {
                    back();
                }
            }
        }
        function moveHandler(e) {

            if (bool_down) {
                if(pageDir=='ver')
                {
                    var mouseY=e.data.global.y;
                    if (sc.pageIndex == 0 && mouseY > mouseDownPT.y) {
                        target.y=targetStartPT.y+(mouseY - mouseDownPT.y)/4;
                    }
                    else if (sc.pageIndex == sc.pageMax && mouseY < mouseDownPT.y) {
                        target.y=targetStartPT.y+(mouseY-mouseDownPT.y)/4;
                    }
                    else {
                        target.y=targetStartPT.y+(mouseY-mouseDownPT.y);
                    }
                }
                else if(pageDir=='hor')
                {
                    var mouseX=e.data.global.x;
                    if (sc.pageIndex == 0 && mouseX > mouseDownPT.x) {
                        target.x=targetStartPT.x+(mouseX - mouseDownPT.x)/4;
                    }
                    else if (sc.pageIndex == sc.pageMax && mouseX < mouseDownPT.x) {
                        target.x=targetStartPT.x+(mouseX-mouseDownPT.x)/4;
                    }
                    else {
                        target.x=targetStartPT.x+(mouseX-mouseDownPT.x);
                    }
                }
            }
        }
        function back()
        {
            if (tw_page) {
                tw_page.stop();
                TWEEN.remove(tw_page);
                tw_page=null;
            }


            if(pageDir=='ver')
            {

                var time = 1 * Math.abs(target.y - (sc.pageIndex * -pageHeight)) / 400;
                TweenMax.to(target,time,{y:sc.pageIndex*-pageHeight,ease:Back.easeOut,onComplete:function () {
                    target.interactive=true;
                }});

            }
            else if(pageDir=='hor')
            {

                var time = 1 * Math.abs(target.x - (sc.pageIndex * -pageWidth)) / 400;
                TweenMax.to(target,time,{x:sc.pageIndex*-pageWidth,ease:Back.easeOut,onComplete:function () {
                    target.interactive=true;
                }});
            }
        }
        return sc;
    }

}